package com.guagua.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guagua.blog.entity.Article;
import com.guagua.blog.entity.Favorite;
import com.guagua.blog.entity.Like;
import com.guagua.blog.entity.ViewLog;
import com.guagua.blog.service.ArticleService;
import com.guagua.blog.service.FavoriteService;
import com.guagua.blog.service.LikeService;
import com.guagua.blog.service.ViewLogService;
import com.guagua.common.response.Result;
import com.guagua.system.dto.PasswordUpdateDTO;
import com.guagua.system.dto.UserUpdateDTO;
import com.guagua.system.entity.User;
import com.guagua.system.security.SecurityUtils;
import com.guagua.system.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "用户管理", description = "用户相关接口")
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {
    
    private final UserService userService;
    private final ArticleService articleService;
    private final LikeService likeService;
    private final FavoriteService favoriteService;
    private final ViewLogService viewLogService;
    
    @Operation(summary = "获取用户信息")
    @GetMapping("/{id}")
    public Result<User> getUserInfo(@PathVariable Long id) {
        User user = userService.getById(id);
        user.setPassword(null);
        return Result.success(user);
    }
    
    @Operation(summary = "更新用户信息")
    @PutMapping("/current")
    public Result<Void> updateUserInfo(@Valid @RequestBody UserUpdateDTO dto) {
        Long userId = SecurityUtils.getCurrentUserId();
        userService.updateUserInfo(userId, dto);
        return Result.success();
    }
    
    @Operation(summary = "修改密码")
    @PutMapping("/current/password")
    public Result<Void> updatePassword(@Valid @RequestBody PasswordUpdateDTO dto) {
        Long userId = SecurityUtils.getCurrentUserId();
        userService.updatePassword(userId, dto);
        return Result.success();
    }
    
    @GetMapping("/stats")
    @Operation(summary = "获取用户统计数据")
    public Result<Map<String, Object>> getUserStats() {
        Long userId = SecurityUtils.getCurrentUserId();
        Map<String, Object> stats = new HashMap<>();
        
        // 获取文章数量
        stats.put("articleCount", articleService.count(new LambdaQueryWrapper<Article>()
            .eq(Article::getAuthorId, userId)));
        
        // 获取总阅读量
        stats.put("viewCount", articleService.sumViewCount(userId));
        
        // 获取获赞数量 (文章获得的赞)
        stats.put("likeCount", likeService.count(new LambdaQueryWrapper<Like>()
            .eq(Like::getArticleAuthorId, userId)));
        
        // 获取收藏数量 (用户收藏的文章数量)
        stats.put("favoriteCount", favoriteService.count(new LambdaQueryWrapper<Favorite>()
            .eq(Favorite::getUserId, userId)));
        
        // 获取阅读文章数 (用户阅读过的文章数量)
        stats.put("readCount", viewLogService.count(new LambdaQueryWrapper<ViewLog>()
            .eq(ViewLog::getUserId, userId)));
        
        // 获取点赞数量 (用户点赞过的文章数量)
        stats.put("userLikeCount", likeService.count(new LambdaQueryWrapper<Like>()
            .eq(Like::getUserId, userId)));
        
        return Result.success(stats);
    }
} 